package com.property.controller;

import java.io.InputStream;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.property.biz.OwnerBiz;
import com.property.biz.UserBiz;
import com.property.entity.Community;
import com.property.entity.Complaint;
import com.property.entity.Management;
import com.property.entity.Owner;
import com.property.entity.User;
import com.property.view.ExcelUtil;
import com.property.view.ExcelView;

@Controller
@RequestMapping("")
public class ExcelController {
	@Autowired
	private OwnerBiz ownerBiz;
	@Autowired
	private UserBiz userBiz;
	
	@RequestMapping("/form")
	public String Form(HttpServletRequest request,HttpSession session) throws Exception{
		MultipartHttpServletRequest multipartHttpServletRequest= (MultipartHttpServletRequest) request;
		InputStream in=null;
		List<List<Object>> listob=null;
		MultipartFile file=multipartHttpServletRequest.getFile("file");
		if(file.isEmpty()){
			throw new Exception("文件不存在！");
		}
		in = file.getInputStream();
		listob=new ExcelUtil().getBank(in, file.getOriginalFilename());
		in.close();
		Management management=(Management) session.getAttribute("management");
		
		for(int i=0;i<listob.size();i++){
			List<Object> lo = listob.get(i);
			Owner owner=new Owner();
			User user=new User();
			Community community=new Community();
			owner.setId((Double.valueOf(lo.get(0).toString())).intValue());
			
			User user2=userBiz.idUser(String.valueOf(lo.get(1)));
			user.setId(user2.getId());
			owner.setUser(user);
			owner.setName(String.valueOf(lo.get(2)));
			owner.setTower(String.valueOf(lo.get(4)));
			owner.setHouseIN(String.valueOf(lo.get(5)));
			owner.setPhone(String.valueOf(lo.get(3)));
			owner.setStatus(String.valueOf(lo.get(6)));
			community.setId(management.getId());
			owner.setCommunity(community);
			Owner owner2=ownerBiz.getbyIds(owner.getId(),management.getId());
			if(owner2==null){
				ownerBiz.addOwner(owner);
			}else{
				ownerBiz.updateOwners(owner);
			}	
		}
		return "redirect:main/houselist";
	}

	@RequestMapping("/dow")
	public ModelAndView downExcel(String key, HttpSession session) {
		List<Owner> list = new ArrayList<Owner>();
		Management mans = (Management) session.getAttribute("management");
		System.out.println(key);
		List<Owner> lists = ownerBiz.getKeyWord(mans.getId(), key, key, key);
		Owner owner[] = new Owner[lists.size()];
		
		for(int i=0;i<lists.size();i++){
			owner[i] = new Owner();
			User user=new User();
			owner[i].setId(lists.get(i).getId());
			user.setUserName(lists.get(i).getUser().getUserName());
			owner[i].setUser(user);
			owner[i].setName(lists.get(i).getName());
			
			owner[i].setPhone(lists.get(i).getPhone());
			owner[i].setTower(lists.get(i).getTower());
			owner[i].setHouseIN(lists.get(i).getHouseIN());
			owner[i].setStatus(lists.get(i).getStatus());
			System.out.println(lists.get(i).getName());
			list.add(owner[i]);
		}
		
		Map<String, List<Owner>> map = new HashMap<String, List<Owner>>();
			map.put("comList", list);
		ExcelView vx=new ExcelView();
		
		return new ModelAndView(vx,map);
	}
}
